import React, { useEffect, useState } from 'react'
import Pubsub from 'pubsub-js'
import Item from './Item'


export default function List() {

  //没发请求时,请开始搜索
  //发请求前,正在搜索
  

  //设置初始化状态
  const [searchList, setSearchList] = useState({isInit:true,isSearch:false,data:[]})
  //设置副作用钩子

  useEffect(() => {



    Pubsub.subscribe('github', (_, data) => {
      // console.log(_, data);
      setSearchList(data)
    })


  }, [])


  //list 接收Header发送的订阅

  //遍历时需要return
  return (
    <div className="row">
      {
       searchList.isInit?<h1>请开始搜索</h1>:
       searchList.isSearch?<h1>正在搜索中</h1>:
       searchList.data.map(item => {
        return <Item key={item.id} {...item} />
       })
      }
    </div>
  )
}
